﻿using System;
using System.Collections.Generic;
using System.Text;
using Young3.FMSearch.Core.Entities.GameVersions;
using Young3.FMSearch.Core.Managers;
using Young3.FMSearch.Core.Offsets;
using System.Linq;
using Young3.FMSearch.Core.Attributes;

namespace Young3.FMSearch.Core.Entities.InGame
{
    //[FMType(UseCache = true)]
    public partial class Player : BaseObject
    {
        public Player(int memoryAddress, IVersion version)
            : base(memoryAddress, version)
        { }
                public Player(int memoryAddress, ArraySegment<byte> originalBytes, IVersion version)
            : base(memoryAddress, originalBytes, version)
        { }

        public void Heal()
        {
            ProcessManager.WriteByte(0, InjuryAddress + 0x1c);
        }

        public Int32 Age { get { return Convert.ToInt32((((TimeSpan)(new Global(Version).IngameDate - DateOfBirth)).TotalDays / 365)); } }

        [FMPointer(PlayerOffsets.Nationality)]
        public Country Nationality { get; set; }

        [FMPointer(PlayerOffsets.ContractAddress)]
        public Contract Contract { get; set; } // sorry, otherwise this is slow as hell

        [FMPointer(PlayerOffsets.ContractAddressSecond)]
        public Contract ContractSecond { get; set; }

        [FMPointer(PlayerOffsets.TeamAddress)]
        public Team Team { get; set; }

        [FMPointer(PlayerOffsets.CityOfBirth)]
        public City CityOfBirth { get; set; }

        #region Skills
        [FMPointer(PlayerOffsets.SkillsAddress)]
        public PlayerSkills Skills { get; internal set; }

        [FMPointer(PlayerOffsets.SkillsAddress)]
        public PositionalSkills PositionalSkills { get; internal set; }

        [FMPointer(PlayerOffsets.SkillsAddress)]
        public NewPositionalSkills NewPositionalSkills { get; internal set; }
        #endregion

        [FMPointer(PlayerOffsets.RelationsAddress)]
        public PlayerRelationContainer Relations { get; set; }

        [FMEntityAttribute(PlayerOffsets.ContractAddress)]
        internal int ContractAddress { get; set; }

        [FMEntityAttribute(PlayerOffsets.ContractAddressSecond)]
        internal int SecondContractAddress { get; set; }

        [FMEntityAttribute(PlayerOffsets.Pa)]
        public Int16 PotentialPlayingAbility { get; set; }

        [FMEntityAttribute(PlayerOffsets.Ca)]
        public Int16 CurrentPlayingAbility { get; set; }

        [FMEntityAttribute(PlayerOffsets.ContractNumber)]
        internal byte ContractNumber { get; set; }

        [FMEntityAttribute(PlayerOffsets.TeamAddress)]
        public int TeamAddress { get; set; }
       
        [FMEntityAttribute(PlayerOffsets.Nationality)]
        private int NationalityAddress { get; set; }

        [FMEntityAttribute(PlayerOffsets.CityOfBirth)]
        private int CityAddress { get; set; }

        [FMEntityAttribute(PlayerOffsets.AskValue)]
        public int SaleValue { get; set; }

        [FMEntityAttribute(PlayerOffsets.DateOfBirth)]
        public DateTime DateOfBirth { get; set; }

        [FMEntityAttribute(PlayerOffsets.InternationalCaps)]
        public byte InternationalCaps { get; set; }

        [FMEntityAttribute(PlayerOffsets.InternationalGoals)]
        public byte InternationalGoals { get; set; }

        [FMEntityAttribute(PlayerOffsets.Height)]
        public byte Height { get; set; }

        [FMEntityAttribute(PlayerOffsets.Morale)]
        public byte Morale { get; set; }

        [FMEntityAttribute(PlayerOffsets.Value)]
        public int Value { get; set; }

        [FMEntityAttribute(PlayerOffsets.U21InternationalCaps)]
        public byte U21InternationalCaps { get; set; }

        [FMEntityAttribute(PlayerOffsets.U21InternationalGoals)]
        public byte U21InternationalGoals { get; set; }

        [FMEntityAttribute(PlayerOffsets.Weight)]
        public byte Weight { get; set; }

        [FMEntityAttribute(PlayerOffsets.CurrentReputation)]
        public Int16 CurrentReputation { get; set; }

        [FMEntityAttribute(PlayerOffsets.HomeReputation)]
        public Int16 HomeReputation { get; set; }

        [FMEntityAttribute(PlayerOffsets.WorldReputation)]
        public Int16 WorldReputation { get; set; }

        [FMEntityAttribute(PlayerOffsets.FirstName, 0x20)]
        public string FirstName { get; internal set; }

        [FMEntityAttribute(PlayerOffsets.LastName, 0x20)]
        public string LastName { get; internal set; }

        [FMEntityAttribute(PlayerOffsets.InjuryAddress)]
        private int InjuryAddress { get; set; }

        [FMEntityAttribute(PlayerOffsets.RelationsAddress)]
        public int RelationsAddress { get; set; }

        [FMEntityAttribute(PlayerOffsets.SkillsAddress)]
        internal int SkillsAddress { get; set; }

        [FMEntityAttribute(PlayerOffsets.Nickname, 0x20)]
        public string Nickname { get; internal set; }

        [FMEntityAttribute(PlayerOffsets.Id)]
        public int ID { get; internal set; }
        
        [FMEntity(PlayerOffsets.Condition)]
        public Int16 Condition { get; set; }

        [FMEntity(PlayerOffsets.Fitness)]
        public Int16 Fitness { get; set; }

        [FMEntity(PlayerOffsets.Jadeness)]
        public Int16 Jadeness { get; set; }

        public int ActiveObjectOffset { get { return 244; } }

        public override string ToString()
        {
            return !string.IsNullOrEmpty(Nickname) ? Nickname : ((string)FirstName + " " + (string)LastName);
        }

    }
}
